package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.text.SimpleDateFormat;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA
 * Descroption
 * User:dell
 * Date:2025-09-08
 * Time:23:15
 */
public class PersonalPage extends Utils {
    public static String url = "http://81.70.248.108:58080/index.html";

    public PersonalPage() {
        super(url);
    }
    /**
     * 检查页面元素
     */
    public void checkPerson() throws InterruptedException {
        webDriver.findElement(By.xpath("//*[@id=\"index_nav_avatar\"]")).click();
        //等待1秒弹窗出现
        Thread.sleep(1000);
        webDriver.findElement(By.xpath("//*[@id=\"index_user_settings\"]")).click();
        //等待页面跳转,并检查页面是否准确
        WebDriverWait personWait = new WebDriverWait(webDriver, Duration.ofSeconds(10));
        personWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div/h2")));
        String boardTitle = webDriver.findElement(By.xpath("//*[@id=\"bit-forum-content\"]/div[1]/div/div/div/h2")).getText();
        assert boardTitle.equals("用户中心"):"跳转不正确";

    }
    public void otherModify() throws InterruptedException {
        //自动生成名字
        SimpleDateFormat sim = new SimpleDateFormat("HHmmssSS");
        String titleTime = sim.format(System.currentTimeMillis());
        WebDriverWait wait = new WebDriverWait(webDriver, Duration.ofSeconds(10));
        //1.修改昵称
        WebElement nickNameEle = webDriver.findElement(By.cssSelector("#setting_input_nickname"));
        String nickNameBefore = nickNameEle.getText();
        wait.until(ExpectedConditions.elementToBeClickable(nickNameEle));
        nickNameEle.click();
        new Actions(webDriver)
                .keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL)
                .sendKeys(Keys.DELETE)
                .perform();
        Thread.sleep(1000);
        webDriver.findElement(By.cssSelector("#setting_input_nickname")).sendKeys("个人昵称"+titleTime);
        Thread.sleep(1000);
        webDriver.findElement(By.cssSelector("#setting_submit_nickname")).click();
        String nickNameAfter = webDriver.findElement(By.cssSelector("#setting_input_nickname")).getText();
        assert !nickNameBefore.equals(nickNameAfter):"更改失败";


        //2. 更改邮箱
        WebElement emailEle = webDriver.findElement(By.cssSelector("#setting_input_email"));
        String emailBefore = emailEle.getText();
        new Actions(webDriver).click(emailEle).perform();
        Thread.sleep(1000);
        new Actions(webDriver)
                .keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL)
                .sendKeys(Keys.DELETE)
                .perform();
        Thread.sleep(1000);
        webDriver.findElement(By.cssSelector("#setting_input_email")).sendKeys(titleTime);
        Thread.sleep(1000);
        webDriver.findElement(By.cssSelector("#setting_submit_email")).click();
//        Thread.sleep(1000);
        String emailAfter = webDriver.findElement(By.cssSelector("#setting_input_email")).getText();
        assert !emailAfter.equals(emailBefore):"更改失败";

        //3.更改电话号
        WebElement phoneEle = webDriver.findElement(By.cssSelector("#setting_input_phoneNum"));
        String phoneBefore = emailEle.getText();
        new Actions(webDriver).click(phoneEle).perform();
        Thread.sleep(1000);
        new Actions(webDriver)
                .keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL)
                .sendKeys(Keys.DELETE)
                .perform();
        Thread.sleep(1000);
        webDriver.findElement(By.cssSelector("#setting_input_phoneNum")).sendKeys(titleTime);
        Thread.sleep(1000);
        webDriver.findElement(By.cssSelector("#setting_submit_phoneNum")).click();
//        Thread.sleep(1000);
        String phoneAfter = webDriver.findElement(By.cssSelector("#setting_input_phoneNum")).getText();
        assert !phoneAfter.equals(phoneBefore):"更改失败";

        //更改个人简介
        WebElement summaryEle = webDriver.findElement(By.cssSelector("#settings_textarea_remark"));
        String summaryBefore = summaryEle.getText();
        new Actions(webDriver).click(summaryEle).perform();
        Thread.sleep(1000);
        new Actions(webDriver)
                .keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL)
                .sendKeys(Keys.DELETE)
                .perform();
        Thread.sleep(1000);
        webDriver.findElement(By.cssSelector("#settings_textarea_remark")).sendKeys("个人简介"+titleTime);
        Thread.sleep(1000);
        webDriver.findElement(By.cssSelector("#settings_submit_remark")).click();
        String summaryAfter = webDriver.findElement(By.cssSelector("#settings_textarea_remark")).getText();
        assert !summaryAfter.equals(summaryBefore);

    }

    /**
     * 修改密码
     */
    public void passwordModifySuc() throws InterruptedException {
        //原密码
        WebElement originalPasEle =  webDriver.findElement(By.cssSelector("#settings_input_oldPassword"));
        originalPasEle.clear();
        originalPasEle.sendKeys("123456");
        Thread.sleep(1000);

        //新密码
        WebElement newPasEle = webDriver.findElement(By.cssSelector("#settings_input_newPassword"));
        newPasEle.clear();
        newPasEle.sendKeys("123456");
        Thread.sleep(1000);

        //确认密码
        WebElement repeatPasEle = webDriver.findElement(By.cssSelector("#settings_input_passwordRepeat"));
        repeatPasEle.clear();
        repeatPasEle.sendKeys("123456");
        Thread.sleep(1000);

        //点击确认
        WebElement submitEle = webDriver.findElement(By.cssSelector("#settings_submit_password"));
        submitEle.click();

    }

    public void passwordModifyFail() throws InterruptedException {
        //1.原密码错误
        WebElement originalPasEle =  webDriver.findElement(By.cssSelector("#settings_input_oldPassword"));
        originalPasEle.clear();
        originalPasEle.sendKeys("12345");
        Thread.sleep(1000);

        //新密码
        WebElement newPasEle = webDriver.findElement(By.cssSelector("#settings_input_newPassword"));
        newPasEle.clear();
        newPasEle.sendKeys("123456");
        Thread.sleep(1000);

        //确认密码
        WebElement repeatPasEle = webDriver.findElement(By.cssSelector("#settings_input_passwordRepeat"));
        repeatPasEle.clear();
        repeatPasEle.sendKeys("123456");
        Thread.sleep(1000);

        //点击确认
        WebElement submitEle = webDriver.findElement(By.cssSelector("#settings_submit_password"));
        submitEle.click();

        //2. 新密码和确认密码不一致
        //1.原密码正确
        WebElement originalPasEle1 =  webDriver.findElement(By.cssSelector("#settings_input_oldPassword"));
        originalPasEle1.clear();
        originalPasEle1.sendKeys("123456");
        Thread.sleep(1000);

        //新密码
        WebElement newPasEle1 = webDriver.findElement(By.cssSelector("#settings_input_newPassword"));
        newPasEle1.clear();
        newPasEle1.sendKeys("123456");
        Thread.sleep(1000);

        //确认密码
        WebElement repeatPasEle1 = webDriver.findElement(By.cssSelector("#settings_input_passwordRepeat"));
        repeatPasEle1.clear();
        repeatPasEle1.sendKeys("12345");
        Thread.sleep(1000);

        //点击确认
        WebElement submitEle1 = webDriver.findElement(By.cssSelector("#settings_submit_password"));
        submitEle1.click();

        //3. 新密码和确认密码不一致，但是原密码错误
        //1.原密码正确
        WebElement originalPasEle2 =  webDriver.findElement(By.cssSelector("#settings_input_oldPassword"));
        originalPasEle2.clear();
        originalPasEle2.sendKeys("12345");
        Thread.sleep(1000);

        //新密码
        WebElement newPasEle2 = webDriver.findElement(By.cssSelector("#settings_input_newPassword"));
        newPasEle2.clear();
        newPasEle2.sendKeys("123456");
        Thread.sleep(1000);

        //确认密码
        WebElement repeatPasEle2 = webDriver.findElement(By.cssSelector("#settings_input_passwordRepeat"));
        repeatPasEle2.clear();
        repeatPasEle2.sendKeys("12345");
        Thread.sleep(1000);

        //点击确认
        WebElement submitEle2 = webDriver.findElement(By.cssSelector("#settings_submit_password"));
        submitEle2.click();
    }
}
